package com.sugar.subscribe.course.module.business.controller.admin.coachvideo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.coachvideo.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachvideo.CoachVideoDO;
import com.sugar.subscribe.course.module.business.service.coachvideo.CoachVideoService;

@Tag(name = "管理后台 - 教练视频介绍")
@RestController
@RequestMapping("/business/coach-video")
@Validated
public class CoachVideoController {

    @Resource
    private CoachVideoService coachVideoService;

    @PostMapping("/create")
    @Operation(summary = "创建教练视频介绍")
    //@PreAuthorize("@ss.hasPermission('business:coach-video:create')")
    public CommonResult<Long> createCoachVideo(@Valid @RequestBody CoachVideoSaveReqVO createReqVO) {
        return success(coachVideoService.createCoachVideo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教练视频介绍")
    //@PreAuthorize("@ss.hasPermission('business:coach-video:update')")
    public CommonResult<Boolean> updateCoachVideo(@Valid @RequestBody CoachVideoSaveReqVO updateReqVO) {
        coachVideoService.updateCoachVideo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教练视频介绍")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:coach-video:delete')")
    public CommonResult<Boolean> deleteCoachVideo(@RequestParam("id") Long id) {
        coachVideoService.deleteCoachVideo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除教练视频介绍")
    //@PreAuthorize("@ss.hasPermission('business:coach-video:delete')")
    public CommonResult<Boolean> deleteCoachVideoList(@RequestParam("ids") List<Long> ids) {
        coachVideoService.deleteCoachVideoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教练视频介绍")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:coach-video:query')")
    public CommonResult<CoachVideoRespVO> getCoachVideo(@RequestParam("id") Long id) {
        CoachVideoDO coachVideo = coachVideoService.getCoachVideo(id);
        return success(BeanUtils.toBean(coachVideo, CoachVideoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教练视频介绍分页")
    //@PreAuthorize("@ss.hasPermission('business:coach-video:query')")
    public CommonResult<PageResult<CoachVideoRespVO>> getCoachVideoPage(@Valid CoachVideoPageReqVO pageReqVO) {
        return success(coachVideoService.getCoachVideoPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教练视频介绍 Excel")
    //@PreAuthorize("@ss.hasPermission('business:coach-video:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoachVideoExcel(@Valid CoachVideoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "教练视频介绍.xls", "数据", CoachVideoRespVO.class,
                coachVideoService.getCoachVideoPage(pageReqVO).getList());
    }

}